<template>
    <div class="gray">
        <div class="good">
            <p>30天无忧退货</p>
            <p>48小时快速退款</p>
            <p>满88元免邮费</p>
        </div>
        <div class="box">
            <ul>
                <li v-for="item in list" :key="item.id">
                    <van-swipe-cell>
                    <div class="left">
                        <van-checkbox v-model="item.checked" checked-color="#ee0a24"></van-checkbox>
                        <img :src="item.list_pic_url" width="62.5" height="62.5">
                        <div class="inner">
                            <p>{{item.goods_name}}</p>
                            <p>￥{{item.retail_price}}</p>
                        </div>
                    </div>
                    <div class="right">x{{item.number}}</div>
                        <template #right>
                            <van-button square text="删除" type="danger" class="delete-button" @click="del(item.id)"/>
                        </template>
                    </van-swipe-cell>
                </li>
               
            </ul>
        </div>
        <div class="fixed">
            <div class="left">
                <van-checkbox v-model="ischecked" checked-color="#ee0a24">全选</van-checkbox>
            </div>
            <div class="right">
                <p>￥{{total}}</p>
                <p @click="addBuy(list)">下单</p>
            </div>
        </div>
        <div class="height"></div>
    </div>

</template>

<script>
import { cartList, deleteAction } from "@/api/cart/index.js"
import { submitAction } from "@/api/home/order/index.js"
import { Dialog } from 'vant';
import { Toast } from 'vant';
export default {
    data() {
        return {
            list:[]
        };
    },
    computed: {
        ischecked:{
            get(){
                if(this.list.length == 0){
                    return false
                }
                return this.list.every(ele=>ele.checked)
            },
            set(val){
                this.list.forEach(ele=>ele.checked = val)
            }
        },
        total(){
            return this.list.reduce((prop,next)=>{
                if(next.checked){
                    return prop + next.retail_price*next.number
                }else{
                    return prop
                }
            },0)
        }
    },
    created() {
        this.init()
    },
    methods: {
        del(id){
            Dialog.confirm({
                title: '提示',
                message: '确定要删除吗?',
                })
                .then(() => {
                    deleteAction({
                        id:id
                    }).then(res=>{
                        this.init();
                    })
                })
                .catch(() => {
                    // on cancel
                });
        },
        addBuy(val){
            var check = this.list.find(ele=>ele.checked)
            if(check){
                var id = this.list.map(ele=>{
                    if(ele.checked){
                        return ele.goods_id
                    }
                }).join(',')
                 submitAction({
                    allPrise:this.total,
                    goodsId:id,
                    openId:localStorage.getItem("openId")
                })
                this.$router.push({path:"/home/info/order"})
            }else{
                Toast('请选择商品');
            }
          

            // this.$store.commit("ids",num);
            // this.$store.commit("totalNum",this.total+"");
            // this.$store.commit("upOrder",val)

            // var id = this.$store.state.id;
            // var num = this.$store.state.nums;
           
        },
        
        init(){
            cartList({
                openId:localStorage.getItem("openId")
            }).then(res=>{
                console.log(res);
                res.data.data.forEach(element => {
                    element.checked = false;
                });
                this.list = res.data.data
                console.log(res.data.data);
            })
        }
    },
    components:{
       
    }
};
</script>

<style scoped lang="scss">
.goods-card {
    margin: 0;
    
  }

  .delete-button {
    height: 100%;
  }

.gray{
    background-color: rgb(244, 244, 244);
}
    .good{
        height: 46px;
        padding: 0 10px;
        padding-left:15px;
        display: flex;
        background-color: rgb(244, 244, 244);
        justify-content: space-between;
        p{
            position: relative;
        }
        p::after{
            content: "";
            display: block;
            position: absolute;
            left: -7px;
            top: 25%;
            width: 3px;
            height: 3px;
            border-radius:50%;
            background-color: rgb(244, 244, 244);
            border: 1px solid #ab2b2b;
        }
    }
    .box{
        width: 100%;
        ul{
            padding: 0 10px;
            background-color: #fff;
            li{
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 10px 0;
                .van-swipe-cell{
                    width: 100%;
                    ::v-deep .van-swipe-cell__wrapper{
                        width: 100%;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                    }
                }
                .left{
                    display: flex;
                    align-items: center;
                    img{
                         background-color: rgb(244, 244, 244);
                         margin-left:10px;
                    }
                    .inner{
                        padding: 10px;
                        display: flex;
                        align-items: center;
                        flex-direction: column;
                        p{
                            text-align: left !important;
                            margin: 8px 0;
                            width: 100%;
                        }
                    }
                }
                .right{
                    margin-right:25px ;
                }
            }
        }

    }
    .height{
        height: 50px;
        background-color: #fff;
    }
    .fixed{
        position: fixed;
        left: 0;
        bottom:50px;
        height: 50px;
        display: flex;
        padding-left: 10px;
        width: 365px;
        background-color: rgb(244, 244, 244);
        justify-content: space-between;
        align-items: center;
        .right{
            display: flex;
            justify-content: space-between;
            align-items: center;
            p{
                color: #b4282d;
            }
            p:last-child{
                text-align: center;
                background: #b4282d;
                color: #fff;
                width: 100px;
                height: 50px;
                margin: 0;
                line-height: 50px;
                margin-left:20px;
            }
        }
    }
</style>
